<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends HYD_Controller {
    
    public function __construct() {
        parent::__construct();
    }

    public function index(){
        //$data['base_url'] = $this->config->base_url();
        //$this->load->view('login',$data);
        if($this->checksession()===TRUE){
            $this->load->view('admin'); 
        }else{
            $this->load->view('login');
        } 
    }
    
    function checklogin(){   
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', '用户名', 'required');
        $this->form_validation->set_rules('password', '密码', 'required');
        $username = $this->input->post('username');
        if ($this->form_validation->run() === FALSE){//对以上验证做判断
            $response = array('success' => FALSE, 'feedback' => '登录失败!');
        }else{
            if( preg_match("/^\d*$/",$username) ){
                $this->load->model('user/user_model');  
                $res = $this->user_model->verify_user(  
                                                $this->input->post('username'),  
                                                $this->input->post('password')  
                                            );  
                if($res !== FALSE){  //print_r($res);
                    $this->admin->login($username);
                    $response = array('success' => TRUE, 'feedback' => '登录成功!');
                }else{
                    $response = array('success' => FALSE, 'feedback' => '登录失败!');
                }  
            }else{
                $response = array('success' => FALSE, 'feedback' => '登录失败,用户名不合法!'); //echo '用户名不存在'; 
            }
        }
        $this->admin->do_userlog(__CLASS__, __FUNCTION__, $username, $response['feedback'], $response['success'], FALSE);
        $this->set_output($response);
    }

    function checksession(){
        if( $this->session->userdata('user_data')){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    function loginout(){
        $this->load->library('session');
        $this->session->unset_userdata('user_data');
        $this->load->view('login');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */